package com.ruoyi.web.controller.wq;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.web.service.QunService;
import com.ruoyi.wq.domain.WqQun;
import com.ruoyi.wq.domain.WqUserQun;
import com.ruoyi.wq.domain.vo.WqFriendApplyVO;
import com.ruoyi.wq.domain.vo.WqQunVO;
import com.ruoyi.wq.service.IWqQunService;
import com.ruoyi.wq.service.IWqUserQunService;

/**
 * 砍价群Controller
 * 
 * @author ruoyi
 * @date 2022-08-09
 */
@RestController
@RequestMapping("/wq/qun")
public class WqQunController extends BaseController{
    
	@Autowired
    private IWqQunService wqQunService;
	
	@Autowired
	private IWqUserQunService wqUserQunService;
	
	@Autowired
	private QunService qunService;

	/**
     * 查询活动列表
     */
    @Anonymous
    @GetMapping("/getList")
    public TableDataInfo getList(WqQun wqQun){
        TableDataInfo info = wqQunService.getPageList(wqQun);
        return info;
    }

    /**
     * 获取砍价群详细信息
     */
    @Anonymous
    @GetMapping(value = "/getDetail")
    public AjaxResult getDetail(WqQun wqQun){
    	AjaxResult ajax = AjaxResult.success();
    	WqQunVO qun = wqQunService.getQunById(wqQun.getId());
    	List<WqUserQun> qunUserList = wqUserQunService.getQunUserListByQunId(wqQun.getId());
    	WqUserQun userQun = wqUserQunService.getIsPay(wqQun.getId(),wqQun.getUserId());
    	boolean isPay = false;
    	if(userQun != null && userQun.getStatus().equals("1")){
    		isPay = true;
    	}
    	
    	if(qun.getFineNum() > qun.getPayNum() || qun.getPayCheck().equals("0")){
			// 免费次数
    		isPay = true;
		}
    	ajax.put("qun", qun);
    	ajax.put("isPay", isPay);
    	ajax.put("qunUserList", qunUserList);
        return ajax;
    }
    
    /**
     * 匹配
     */
    @PostMapping("/join")
    public AjaxResult join(@RequestBody WqQun wqQun){
    	WqFriendApplyVO qun = qunService.joinQun(wqQun);
        return AjaxResult.success(qun);
    }
    
    /**
     * 创建群
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WqQun wqQun){
    	qunService.add(wqQun);
        return AjaxResult.success();
    }

}
